﻿$(function () {
    //当页面首次刷新的时候执行的事件 
    if ($("#hSourceType").val() == '1' || ($("#hSourceType").val() == '2' && $("#hHandleState").val() == '0')) {
        var trCommom = document.getElementById('trCommon');
        var table = trCommom.parentNode;
        table.removeChild(trCommom);

        $("#StatisticalCycle").combobox({
            editable: false, panelHeight: "auto",
            valueField: "DictionaryName",
            textField: "DictionaryName",
            url: "/Dictionary/GetDictionaryByTypeTag?typeTag=TenStage"
        });
    }
    else {
        var trRequest = document.getElementById('trRequest');
        var table = trRequest.parentNode;
        table.removeChild(trRequest);
    }

    initBillTable();
    initFlowTable();
});

function initBillTable() {
    var sourceType = $("#hSourceType").val();
    var processType = $("#hProcessType").val();
    if (sourceType == "1" || (sourceType == "2" && processType == "1")) {
        $('#stageScheduleDetailsDataGrid').datagrid({
            url: '/StageSchedule/GetStageScheduleDetailListForFlow?projectID=' + $("#hProjectID").val() + '&contractType=' + encodeURI($("#hContractType").val()) + '&contractID=' + $("#hContractID").val() + '&year=' + $("#hYear").val() + '&month=' + $("#hMonth").val() + '&stageScheduleID=' + $("#hID").val(),
            iconCls: 'icon-bricks',
            nowrap: true,//当数据长度超出列宽时将会自动截取
            autoRowHeight: false,
            striped: true,//设置为true将交替显示行背景
            collapsible: true,
            //pagination: true,
            //pageSize: 20,
            remoteSort: false,
            idField: 'ID',//表明该列是一个唯一列。
            singleSelect: true,
            fitColumns: true,
            columns:
            [
                [
                    { title: '父项目名称', field: 'ParentStatisticsItemName', align: 'center', rowspan: 3, width: 100 },
                    { title: '项目名称', field: 'StatisticsItemName', align: 'center', rowspan: 3, width: 100 },
                    { title: '单位', field: 'Unit', align: 'center', rowspan: 3, width: 50 },
                    { title: '总工程量', field: '', width: 200 },
                    { title: '本月计划', field: '', width: 200 },
                    { title: '阶段完成', field: '', width: 200 }

                ],
                [

                     { title: '金额', field: 'TotalAmount', align: 'center', rowspan: 2, width: 200 },
                     { title: '工程量', field: '', width: 200 },
                     { title: '工程量', field: '', width: 200 }
                ],
                [
                     { title: '金额', field: 'CurrentMonthPlanAmount', align: 'center', width: 200 },
                     { title: '金额', field: 'StageCompletedAmount', align: 'center', width: 200, editor: 'text' }
                ]
            ],
            onLoadSuccess: function (data) {
                if (data.rows.length > 1) {
                    mergeCellsByField("stageScheduleDetailsDataGrid", "ParentStatisticsItemName,StatisticsItemName");
                }
                if (data.rows.length > 0) {
                    mergeColCells("stageScheduleDetailsDataGrid");
                }
                var billList = data.rows;
                for (var i = 0; i < billList.length; i++) {
                    $('#stageScheduleDetailsDataGrid').datagrid('beginEdit', i);
                }
            }
        });
    }
    else {
        $('#stageScheduleDetailsDataGrid').datagrid({
            url: '/StageSchedule/GetStageScheduleDetailListForFlow?projectID=' + $("#hProjectID").val() + '&contractType=' + encodeURI($("#hContractType").val()) + '&contractID=' + $("#hContractID").val() + '&year=' + $("#hYear").val() + '&month=' + $("#hMonth").val() + '&stageScheduleID=' + $("#hID").val(),
            iconCls: 'icon-bricks',
            nowrap: true,//当数据长度超出列宽时将会自动截取
            autoRowHeight: false,
            striped: true,//设置为true将交替显示行背景
            collapsible: true,
            //pagination: true,
            //pageSize: 20,
            remoteSort: false,
            idField: 'ID',//表明该列是一个唯一列。
            singleSelect: true,
            fitColumns: true,
            columns:
            [
                [
                    { title: '父项目名称', field: 'ParentStatisticsItemName', align: 'center', rowspan: 3, width: 100 },
                    { title: '项目名称', field: 'StatisticsItemName', align: 'center', rowspan: 3, width: 100 },
                    { title: '单位', field: 'Unit', align: 'center', rowspan: 3, width: 50 },
                    { title: '总工程量', field: '', width: 200 },
                    { title: '本月计划', field: '', width: 200 },
                    { title: '阶段完成', field: '', width: 200 }

                ],
                [

                     { title: '金额', field: 'TotalAmount', align: 'center', rowspan: 2, width: 200 },
                     { title: '工程量', field: '', width: 200 },
                     { title: '工程量', field: '', width: 200 }
                ],
                [
                     { title: '金额', field: 'CurrentMonthPlanAmount', align: 'center', width: 200 },
                     { title: '金额', field: 'StageCompletedAmount', align: 'center', width: 200 }
                ]
            ],
            onLoadSuccess: function (data) {
                if (data.rows.length > 1) {
                    mergeCellsByField("stageScheduleDetailsDataGrid", "ParentStatisticsItemName,StatisticsItemName");
                }
                if (data.rows.length > 0) {
                    mergeColCells("stageScheduleDetailsDataGrid");
                }
            }
        });
    }
}

function initFlowTable() {
    var id = $("#hID").val();
    $('#myDataGrid').datagrid({
        url: '/ProcessedTask/ProcessedTaskList?recordID=' + id,
        fitColumns: true,
        nowrap: true,
        autoRowHeight: false,
        striped: true,
        collapsible: true,
        pagination: true,
        pageSize: 20,
        rownumbers: true,
        remoteSort: false,
        idField: 'ID',
        singleSelect: true,
        columns: [[
                    { title: '节点名称', field: 'CurrentProcessNodeName', width: 200, sortable: true },
                    { title: '审批意见', field: 'ApprovalOpinion', width: 300, sortable: true },
                    { title: '处理人部门', field: 'CurrentProcessPersonDepartmentName', width: 100, sortable: true },
                    { title: '处理人', field: 'CurrentProcessPersonName', width: 100, sortable: true },
                    { title: '处理时间', field: 'ProcessTimeFormat', width: 100, sortable: true }
        ]]
    });

    $('#myCirculatedDataGrid').datagrid({
        url: '/FlowProcessMessage/GetFlowProcessMessageListForReceipt?recordID=' + id + "&processType=1",
        iconCls: 'icon-bricks',
        nowrap: true,
        autoRowHeight: false,
        striped: true,
        collapsible: true,
        pagination: true,
        pageSize: 20,
        remoteSort: false,
        idField: 'ID',
        singleSelect: true,
        columns: [[
                    { title: '描述', field: 'DisplayText', width: 600, sortable: true },
                    { title: '时间', field: 'AssociatedDate', width: 200, sortable: true }
        ]]
    });

    $('#myReceiptDataGrid').datagrid({
        url: '/FlowProcessMessage/GetFlowProcessMessageListForReceipt?recordID=' + id + "&processType=2",
        iconCls: 'icon-bricks',
        nowrap: true,
        autoRowHeight: false,
        striped: true,
        collapsible: true,
        pagination: true,
        pageSize: 20,
        remoteSort: false,
        idField: 'ID',
        singleSelect: true,
        columns: [[
                    { title: '描述', field: 'DisplayText', width: 600, sortable: true },
                    { title: '时间', field: 'AssociatedDate', width: 200, sortable: true }
        ]]
    });
}

var StageSchedule =
    {
        Save: function () {
            $('#stageScheduleDetailsDataGrid').datagrid('acceptChanges');
            var stageScheduleDetailList = $('#stageScheduleDetailsDataGrid').datagrid('getData').rows;
            var stageScheduleDetailListJson = JSON.stringify(stageScheduleDetailList);
            $('#hStageScheduleDetailListJson').val(stageScheduleDetailListJson);
            $('#frmStageSchedule').form('submit', {
                url: '/StageSchedule/Save',
                onSubmit: function () {

                },
                success: function (data) {
                    if (data != "保存成功！") {
                        $.EasyuiSlide(data);
                    }
                    else {
                        $('#btnCirculation').linkbutton('enable');
                        $('#btnCirculated').linkbutton('enable');
                        $.EasyuiSlide(data);
                    }
                    if (stageScheduleDetailList.length > 1) {
                        mergeCellsByField("stageScheduleDetailsDataGrid", "ParentStatisticsItemName,StatisticsItemName");
                    }
                    if (stageScheduleDetailList.length > 0) {
                        mergeColCells("stageScheduleDetailsDataGrid");
                    }
                    for (var i = 0; i < stageScheduleDetailList.length; i++) {
                        $('#stageScheduleDetailsDataGrid').datagrid('beginEdit', i);
                    }
                },
                fail: function (data) {
                    $.EasyuiSlide(data);
                }
            });
        },

        Circulation: function () {
            var flowCode = $("#hFlowCode").val();
            var id = $("#hID").val();
            var handleState = $("#hHandleState").val();
            var nodeCode = $("#hAssociatedNodeCode").val();
            var processUserDepartment = $("#hLastUpdateUserDepartment").val();
            var processUser = $("#hLastUpdateUser").val();

            showMyWindow("#stageScheduleWindow", "流程审批", "/Flow/CirculationFlow?flowCode=" + flowCode + "&recordID=" + id + "&handleState=" + handleState + "&currentFlowNodeCode=" + nodeCode + "&processUserDepartment=" + processUserDepartment + "&processUser=" + processUser, 500, 250, true, false);
        },

        Withdraw: function () {
            var withdrawNodeCode = $("#hProcessdOrCirculatedNodeCode").val();
            $('#frmStageSchedule').form('submit', {
                url: '/StageSchedule/Withdraw?withdrawNodeCode=' + withdrawNodeCode,
                onSubmit: function () {

                },
                success: function (data) {
                    window.parent.CloseWindow('撤回成功！');
                },
                fail: function (data) {
                    $.EasyuiSlide(data);
                }
            });
        },

        Back: function () {
            var flowCode = $("#hFlowCode").val();
            var id = $("#hID").val();
            var nodeCode = $("#hAssociatedNodeCode").val();
            showMyWindow("#stageScheduleWindow", "流程回退", "/Flow/BackFlow?flowCode=" + flowCode + "&recordID=" + id + "&flowNodeCode=" + nodeCode, 550, 400, true, false);
        },

        Cancel: function () {
            var flowCode = $("#hFlowCode").val();
            var id = $("#hID").val();
            $.post("/StageSchedule/Cancel?flowCode=" + flowCode + "&id=" + id, function (res) {
                if (res == "Success") {
                    window.parent.CloseWindow('终止成功！');
                }
                else {
                    $.EasyuiSlide(res);
                }
            });
        },

        Circulated: function () {
            var flowCode = $("#hFlowCode").val();
            var id = $("#hID").val();
            var nodeCode = $("#hProcessdOrCirculatedNodeCode").val();
            var department = $("#hProcessdOrCirculatedUserDepartment").val();
            showMyWindow("#stageScheduleWindow", "流程传阅", "/Flow/CirculatedFlow?flowCode=" + flowCode + "&recordID=" + id + "&nodeCode=" + nodeCode + "&department=" + department, 400, 400, true, false);
        },

        Receipt: function () {
            var flowCode = $("#hFlowCode").val();
            var messageID = $("#hSourceID").val();
            showMyWindow("#stageScheduleWindow", "流程已阅", "/Flow/ReceiptFlow?flowCode=" + flowCode + "&messageID=" + messageID, 500, 250, true, false);
        },

        Reminders: function () {
            var flowCode = $("#hFlowCode").val();
            var id = $("#hID").val();
            $.post("/StageSchedule/Reminders?flowCode=" + flowCode + "&id=" + id, function (res) {
                if (res == "Success") {
                    window.parent.CloseWindow('催办成功！');
                }
                else {
                    $.EasyuiSlide(res);
                }
            });
        }
    }

function CloseSelf() {
    var handleState = $("#hHandleState").val();
    window.parent.cancelPopWindow(handleState);
}

function PreviewProcessGraph(title, index) {
    if (index == 1) {
        var flowCode = $("#hFlowCode").val();
        var id = $("#hID").val();
        $.post("/StageSchedule/GenerateGraph?flowCode=" + flowCode + "&id=" + id, function (res) {
            var container = document.getElementById('graphContainer');
            if (container.hasChildNodes()) {
                $("#graphContainer").empty()
            }
            PreviewGraph(container, res);
        }
        )
    }
}

//关闭弹出窗口，按条件刷新主界面
//0：取消，1：发送，2：回退，3：传阅，4：已阅
function CloseWindow(closeType, data) {
    colseMyWindow("#stageScheduleWindow");
    if (closeType != 0) {
        window.parent.CloseWindow(data);
    }
}

function cancelPopWindow() {
    colseMyWindow("#stageScheduleWindow");
}
